ArticleMainWindow = {
	_templetForArticleCategoryListItem:'',
	_templetForArticleListItem:'',
	currentCategory:0
}
ArticleMainWindow.init = function () {
    windowManager.newWindow('article-and-category-list-container', 'articleMain');

    ArticleMainWindow._templetForArticleCategoryListItem
        = getSelfHtml("#article-category-list-container [templete='articleCategoryItem']");
    $("#article-category-list-container [templete='articleCategoryItem']").remove();

    ArticleMainWindow._templetForArticleListItem
        = getSelfHtml("#article-list-container [templete='articleListItem']");
    $("#article-list-container [templete='articleListItem']").remove();

    $('#article-category-list-item-action-add')
		.button()
		.click(function () {
			ArticleMainWindow._onCategoryAddClicked();
		});

    $('#article-list-write-new')
		.button()
		.click(function () {
			ArticleMainWindow._onWriteNewClicked();
		});
	
    return ArticleMainWindow;
};
ArticleMainWindow.open = function(){
	windowManager.windowShow('articleMain', ArticleMainWindow);
};
ArticleMainWindow.close = function(){
	windowManager.windowHide('articleMain');
	ArticleMainWindow._newArticleItemUnbindEvents();
	ArticleMainWindow._newCategoryItemUnbindEvents();
}
ArticleMainWindow.refreshCategoryList = function(){
	$('#article-category-list-container').empty();
	
	for (var i=0; i<Article.category.categories.length; i++){
		ArticleMainWindow._addCategoryItem(Article.category.categories[i]);
	}
};
ArticleMainWindow.refreshArticleList = function(){
	ArticleMainWindow.currentCategory *= 1;
	
	$('#article-list-container').empty();
	
	for ( var i=0; i<Article.articles.length; i++ ){
		if ( Article.articles[i].category == ArticleMainWindow.currentCategory )
			ArticleMainWindow._addArticleItem( Article.articles[i] );
	}
};

ArticleMainWindow._onWriteNewClicked = function(){
	ArticleMainWindow.close();

	ArticleEditWindow.articleId = 0;
	ArticleEditWindow.returnBackTo = 'articleMain';
	ArticleEditWindow.open();
};
ArticleMainWindow._onCategoryAddClicked = function(){
	if ( '' == $.trim($('#article-category-list-new-name').val()) ){
		for(var i=0; i<3; i++)
			$('#article-category-list-new-name').effect('highlight', {}, 500, function(){});
		return;
	}
	
	Article.category.create($('#article-category-list-new-name').val(), function(){
		ArticleMainWindow.refreshCategoryList();
		$('#article-category-list-new-name').val('');
	});	
};
ArticleMainWindow._onCategoryItemClicked = function(elem){
	Article.listByCategory(elem.attr('category'), function(){
		ArticleMainWindow.currentCategory = elem.attr('category');
		ArticleMainWindow.refreshArticleList();
	});
};
ArticleMainWindow._onCategoryDownloadMouseEnter = function(elem){
	var downloadButton = elem.children().eq(1).children().eq(0);
	downloadButton.addClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onCategoryDownloadMouseLeave = function(elem){
	var downloadButton = elem.children().eq(1).children().eq(0);
	downloadButton.removeClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onCategoryDownloadClicked = function(elem){
	Article.downloadByCategory(elem.attr('category'), function(url){
		window.open(url);
	});
};
ArticleMainWindow._onCategoryRenameMouseEnter = function(elem){
	var renameButton = elem.children().eq(1).children().eq(1);
	renameButton.addClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onCategoryRenameMouseLeave= function(elem){
	var renameButton = elem.children().eq(1).children().eq(1);
	renameButton.removeClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onCategoryRenameClicked = function(elem){
	var editor = $('<input type="text" class="ui-corner-all" style="width:99%"/>');
	var nameContainer = elem.children().eq(0);
	var catInfo = Article.category.getInfo(elem.attr('category'));
	
	nameContainer.empty().append(editor);
	editor.val(catInfo.name).focus();
	editor
		.click(function(){return false;})
		.keydown(function(e){
			if (13 == e.keyCode){
				ArticleMainWindow._onCategoryItemRenameFinished(elem, editor);
				return false;
			}
			editor.attr('changed', 'true');
			return true;
		});
};
ArticleMainWindow._onCategoryItemRenameFinished = function( elem, editor ){
	if ( $.trim(editor.val()).length > 0 && 'true' === editor.attr('changed') ){
		editor.remove();
		Article.category.rename( elem.attr('category'), $.trim(editor.val()), function(){ArticleMainWindow.refreshCategoryList();});
	}else{
		editor.remove();
		ArticleMainWindow.refreshCategoryList();
	}
}
ArticleMainWindow._onCategoryItemMouseEnter = function(elem){
	elem.addClass("ui-state-hover ui-corner-all");
}
ArticleMainWindow._onCategoryItemMouseLeave = function(elem){
	elem.removeClass("ui-state-hover ui-corner-all");
}
ArticleMainWindow._onOpened = function(){
	if ( 0 == Article.category.categories.length )
		Article.category.list(ArticleMainWindow.refreshCategoryList);
	else
		ArticleMainWindow.refreshCategoryList();
		
	ArticleMainWindow.refreshArticleList();
};
ArticleMainWindow._onListItemClicked = function(elem) {
	ArticleMainWindow.close();
	
	ArticleReadWindow.articleId = elem.attr('article');
	ArticleReadWindow.open();
};
ArticleMainWindow._onListItemMouseEnter = function(elem){
	elem.addClass("ui-state-hover ui-corner-all");
}
ArticleMainWindow._onListItemMouseLeave = function(elem){
	elem.removeClass("ui-state-hover ui-corner-all");
}
ArticleMainWindow._onListItemLockClicked = function(elem){
	Article.lock(elem.attr('article'), function(){
		elem.children().eq(1).children().eq(0).hide();
		elem.children().eq(1).children().eq(1).hide();
	});
};
ArticleMainWindow._onListItemLockMouseEnter = function(elem){
	var lockButton = elem.children().eq(1).children().eq(0)
	lockButton.addClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemLockMouseLeave = function(elem){
	var lockButton = elem.children().eq(1).children().eq(0);
	lockButton.removeClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemEditClicked = function(elem){
	ArticleMainWindow.close();

	ArticleEditWindow.articleId = elem.attr('article');
	ArticleEditWindow.returnBackTo = 'articleRead';
	ArticleEditWindow.open();
};
ArticleMainWindow._onListItemEditMouseEnter = function(elem){
	var editButton = elem.children().eq(1).children().eq(1);
	editButton.addClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemEditMouseLeave = function(elem){
	var editButton = elem.children().eq(1).children().eq(1);
	editButton.removeClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemDownloadClicked = function(elem){
	Article.downloadById(elem.attr('article'), function(url){
		window.open(url);
	});
};
ArticleMainWindow._onListItemDownloadMouseEnter = function(elem){
	var downloadButton = elem.children().eq(1).children().eq(2);
	downloadButton.addClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemDownloadMouseLeave = function(elem){
	var downloadButton = elem.children().eq(1).children().eq(2);
	downloadButton.removeClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemMoveClicked = function(elem){
	elem.children().eq(1).children().eq(3).hide();
	
	var selector = $('<select>', {class:'ui-state-default ui-corner-all'});
	
	elem.children().eq(1).append(selector);
	
	for ( var i=0; i<Article.category.categories.length; i++ )
		$('<option />', 
		{
			value:Article.category.categories[i].id, 
			html:Article.category.categories[i].name
		}).appendTo(selector);
				
	var art = Article.getInfo(elem.attr('article'));
	var oldCategory = art.category;
	
	selector
		.click(function(){return false;})
		.val(oldCategory)
		.change(function(){
			selector.remove();
			elem.children().eq(1).children().eq(3).show();
			
			Article.move( elem.attr('article'), selector.val(), function(){
                ArticleMainWindow.refreshCategoryList();
                ArticleMainWindow.refreshArticleList();
			});
		})
		.blur(function(){
			selector.remove();
			elem.children().eq(1).children().eq(3).show();
		});
};
ArticleMainWindow._onListItemMoveMouseEnter = function(elem){
	var moveButton = elem.children().eq(1).children().eq(3);
	moveButton.addClass('ui-widget-header ui-corner-all');
}
ArticleMainWindow._onListItemMoveMouseLeave = function(elem){
	var moveButton = elem.children().eq(1).children().eq(3);
	moveButton.removeClass('ui-widget-header ui-corner-all');
}

ArticleMainWindow._addCategoryItem = function(categoryInfo ){
	var templete = ArticleMainWindow._templetForArticleCategoryListItem;
	templete = templete.replace(/{categoryId}/g, categoryInfo.id);
	templete = templete.replace(/{categoryName}/g, categoryInfo.name);
	templete = templete.replace(/{categorySonCount}/g, categoryInfo.sonCount);
	
	var elem = $(templete);
	elem.appendTo('#article-category-list-container');
	
	ArticleMainWindow._newCategoryItemBindEvents(elem);
};
ArticleMainWindow._newArticleItemBindEvents = function( elem ){
	elem
		.click(function(){ArticleMainWindow._onListItemClicked(elem);})
		.mouseenter(function(){ArticleMainWindow._onListItemMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onListItemMouseLeave(elem)});
		
	elem.children().eq(1).children().eq(0)
		.click(function(){ArticleMainWindow._onListItemLockClicked(elem);return false;})
		.mouseenter(function(){ArticleMainWindow._onListItemLockMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onListItemLockMouseLeave(elem)});
		
	elem.children().eq(1).children().eq(1)
		.click(function(){ArticleMainWindow._onListItemEditClicked(elem);return false;})
		.mouseenter(function(){ArticleMainWindow._onListItemEditMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onListItemEditMouseLeave(elem)});
		
	elem.children().eq(1).children().eq(2)
		.click(function(){ArticleMainWindow._onListItemDownloadClicked(elem);return false;})
		.mouseenter(function(){ArticleMainWindow._onListItemDownloadMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onListItemDownloadMouseLeave(elem)});
	
	elem.children().eq(1).children().eq(3)
		.click(function(){ArticleMainWindow._onListItemMoveClicked(elem);return false;})
		.mouseenter(function(){ArticleMainWindow._onListItemMoveMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onListItemMoveMouseLeave(elem)});
};
ArticleMainWindow._newArticleItemUnbindEvents = function( elem ){
	$('.article-list-item').unbind('click').unbind('mouseenter').unbind('mouseleave');
	
	$('.article-list-item-action-lock').unbind('click').unbind('mouseenter').unbind('mouseleave');
	
	$('.article-list-item-action-edit').unbind('click').unbind('mouseenter').unbind('mouseleave');
	
	$('.article-list-item-action-download').unbind('click').unbind('mouseenter').unbind('mouseleave');
	
	$('.article-list-item-action-move').unbind('click').unbind('mouseenter').unbind('mouseleave');
}
ArticleMainWindow._newCategoryItemBindEvents = function( elem ){
	elem
		.click(function(){ArticleMainWindow._onCategoryItemClicked(elem);})
		.mouseenter(function(){ArticleMainWindow._onCategoryItemMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onCategoryItemMouseLeave(elem)});

	elem.children().eq(1).children().eq(0)
		.click(function(){ArticleMainWindow._onCategoryDownloadClicked(elem);return false;})
		.mouseenter(function(){ArticleMainWindow._onCategoryDownloadMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onCategoryDownloadMouseLeave(elem)});
	
	elem.children().eq(1).children().eq(1)
		.click(function(){ArticleMainWindow._onCategoryRenameClicked(elem);return false;})
		.mouseenter(function(){ArticleMainWindow._onCategoryRenameMouseEnter(elem)})
		.mouseleave(function(){ArticleMainWindow._onCategoryRenameMouseLeave(elem)});
};
ArticleMainWindow._newCategoryItemUnbindEvents = function( elem ){
	$('.article-category-list-item').unbind('click').unbind('mouseenter').unbind('mouseleave');
	
	$('.article-category-list-item-action-download').unbind('click').unbind('mouseenter').unbind('mouseleave');;
	
	$('.article-category-list-item-action-rename').unbind('click').unbind('mouseenter').unbind('mouseleave');;
}
ArticleMainWindow._addArticleItem = function( articleInfo ){
	var templete = ArticleMainWindow._templetForArticleListItem;
	templete = templete.replace(/{articleId}/g, articleInfo.id);
	templete = templete.replace(/{articleTitle}/g, articleInfo.title);
	templete = templete.replace(/{articleWroteAt}/g, articleInfo.wroteAt);
	
	var elem = $(templete);
	
	if ( articleInfo.state == Article.STATE.LOCKED ){
		elem.children().eq(1).children().eq(0).hide();
		elem.children().eq(1).children().eq(1).hide();
	}
	
	elem.appendTo('#article-list-container');

	ArticleMainWindow._newArticleItemBindEvents(elem);
};
